function [EC] = makeSERDS(filename1,filename2,setoff,settype,setplot,vargin)
%% MAKESERDS(filename,setoff,settype,setplot)  --  Make fake SERDS dataset
% Takes two datasets acquired with same settings but different SERDS lasers
% and combines them into an APE SERDS dataset.
%
% !! ATTENTION: File with higher wavelength Laser has to be declared as
%    the first filename !!
%
% INPUT:    filename    : filename of dataset to be used as template
%           setoff      : amount of pixels to shift
%           settype     : 0=[offset in Pixel], 1=[offset in wavelength (nm)]
%           setplot     : set to 0 to avoid plot
%
% OUTPUT:   EC
%

%  (c) F. Dworkowski, 2012
%      Version 1.0, 2012-08-03

%% CONFIG
if nargin < 2 || nargin > 5
    help makeSERDS
    EC = 1; % exit with EC indicating ERROR
    return
elseif nargin < 3
    setoff      = 0; % setoff of SERDS lasers
    settype     = 0;
    setplot     = 1;
elseif nargin < 4
    settype     = 0;
    setplot     = 1;
elseif nargin < 5
    setplot     = 1;
end

%% CODE
EC=0;
dataset1=load(filename1);
dataset2=load(filename2);
if dataset1.datax ~= dataset2.datax
    EC=1;
    error('ERROR: Dataset must contain same Xaxis data!');
    return
end

%% Scale if neceassary

mean1=mean(dataset1.datay(1:10));
mean2=mean(dataset2.datay(1:10));
diff12=mean1-mean2;
diff21=mean2-mean1;
if round(mean1) < round(mean2)
    dataset1.datay=dataset1.datay+diff21;
    warning('SCALING WAS NEEDED for dataset1!')
elseif round(mean1) > round(mean2)
    dataset2.datay=dataset2.datay+diff12;
    warning('SCALING WAS NEEDED for dataset2!')
else
    disp('No scaling was needed.')
end

%% find setoff if necessary

if setoff == 0
    [~, max1I] = max(dataset1.datay);
    [~, max2I] = max(dataset2.datay);
    setoff = abs(max1I-max2I);
    settype = 0;
    disp(['SETOFF between spectra was autmatically determined @ ',num2str(setoff),' pixel'])
else
    EC=1;
    error('Setoff was neither defined nor could be automatically determined')
end
%% Make SERDS dataset
SERDSdata.datax = dataset2.datax;
SERDSdata.datay = dataset2.datay-dataset1.datay;
SERDSdata.setoff = setoff;
if settype == 1
    SERDSdata.setofftype = 'Wavelength';
else
    SERDSdata.setofftype = 'Pixel';
end

%% plot data for debug
if setplot == 1
    figure(77)
        subplot(2,1,1)
            plot(dataset1.datax, dataset1.datay,'r')
            hold on
            plot(dataset2.datax, dataset2.datay,'b')
            hold off
        subplot(2,1,2)
            plot(SERDSdata.datax, SERDSdata.datay,'k')
end
        
%% save new Datasets

% save([filename,'_SERDS1'],'-struct','dataset1');
%     disp([filename,'_SERDS1.mat saved.']);
% save([filename,'_SERDS2'],'-struct','dataset2');
%     disp([filename,'_SERDS2.mat saved.']);
save(['SERDS_',filename1],'dataset1','dataset2','SERDSdata');
    disp(['SERDS_',filename1,'.mat saved.']);
    

EC = 0; % exit with EC indicating OK
        
        